﻿
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using IotFun.Const;
using IotFun.Entity;
using IotFun.Views.EquipGroupView.Dto;
using Mapster;
using Microsoft.AspNetCore.Mvc;

namespace IotFun.Views.EquipGroupView;
/// <summary>
/// 设备分组服务
/// </summary>
[ApiDescriptionSettings(IotConst.GroupName)]
public class EquipGroupService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<EquipGroup> _rep;
    public EquipGroupService(SqlSugarRepository<EquipGroup> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询设备分组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<EquipGroupOutput>> Page([FromQuery] EquipGroupInput input)
    {
        var query = _rep.AsQueryable()
                    .WhereIF(!string.IsNullOrWhiteSpace(input.name), u => u.Name.Contains(input.name.Trim()))

                    .Select<EquipGroupOutput>()
;
        query = query.OrderBuilder(input);
        var datas= await query.ToPagedListAsync(input.Page, input.PageSize);

        var groupCounts = await _rep.Context.Queryable<EquipInfo>().Where(x => x.IsDelete == false).GroupBy(x => x.GroupId).Select(group => new { group.GroupId, Count = SqlSugar.SqlFunc.AggregateCount(group.ProgramId) }).ToListAsync();

        foreach(var item in datas.Items)
        {
            var group = groupCounts.FirstOrDefault(x => x.GroupId == item.id);
            item.TotalEquips = group?.Count;
        }

        return datas;
    }

    /// <summary>
    /// 增加设备分组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddEquipGroupInput input)
    {
        var entity = input.Adapt<EquipGroup>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除设备分组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteEquipGroupInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.DeleteAsync(entity); 
    }

    /// <summary>
    /// 更新设备分组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateEquipGroupInput input)
    {
        var entity = input.Adapt<EquipGroup>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取设备分组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<EquipGroup> Get([FromQuery] QueryByIdEquipGroupInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.id);
    }

    /// <summary>
    /// 获取设备分组列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<EquipGroupOutput>> List([FromQuery] EquipGroupInput input)
    {
        return await _rep.AsQueryable().Select<EquipGroupOutput>().ToListAsync();
    }





}

